package server.controller.dao.impl;

import common.model.entity.User;
import server.DataBuffer;
import server.controller.dao.BaseDao;
import server.controller.dao.UserDao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public User queryById(Integer userId) {
        String sql = "select * from tb_user where user_id="+userId;
        try {
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);
            while (rs.next()) {
                User u = new User();
                u.setId(Long.parseLong(rs.getString("user_id")));
                u.setPassword(rs.getString("user_pwd"));
                u.setNickname(rs.getString("user_name"));
                u.setSex(rs.getString("user_sex"));
                return u;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int queryKey() {
        String sql = "select * from tb_key";
        try {
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);
            while (rs.next()) {
                int key = rs.getInt(1);
                return key;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User queryByPwd(String userPwd) {
        String sql = "select * from tb_user where user_pwd="+userPwd;
        try {
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);
            while (rs.next()) {
                User u = new User();
                //u.setId(Long.parseLong(rs.getString("user_id")));
                u.setPassword(rs.getString("user_pwd"));
                u.setNickname(rs.getString("user_name"));
                u.setSex(rs.getString("user_sex"));
                u.setId(Long.parseLong(DataBuffer.configProp.getProperty("key")));
                return u;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> queryAll() {
        String sql = "select * from tb_user";
        List<User> list = new ArrayList<>();
        try {
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            while (rs.next()) {
                User u = new User();
                u.setId(Long.parseLong(rs.getString("user_id")));
                u.setPassword(rs.getString("user_pwd"));
                u.setNickname(rs.getString("user_name"));
                u.setSex(rs.getString("user_sex"));
                list.add(u);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean insert(User user) {
        String sql = "insert into tb_user values(?,?,?,null,1)";
        try {
            PreparedStatement sta = con.prepareStatement(sql);
            sta.setInt(1,Integer.parseInt(user.getId().toString()));
            sta.setString(2,user.getPassword());
            sta.setString(3,user.getNickname());
            if (sta.executeUpdate() >= 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(User user) {
        String sql = "update tb_user set user_sex=? where user_id=?";
        try {
            PreparedStatement sta = con.prepareStatement(sql);
            sta.setString(1,user.getPassword());
            sta.setString(2,user.getNickname());
            if (sta.executeUpdate() >= 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateKey() {

        String sql = "update tb_key set user_key=? where key=?";
        try {
            PreparedStatement sta = con.prepareStatement(sql);
            int key =queryKey();
            sta.setInt(2, ++key);
            sta.setInt(1, ++key);
            if (sta.executeUpdate() >= 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int deleteById(Integer userId) {
        return 0;
    }
}
